package de.backtomedieval.units;

import java.util.Random;

import de.backtomedieval.R;
import de.backtomedieval.main.Game;
import de.backtomedieval.research.Technology;

//position @ Array
// Archer // Short // Long // 2-Hand // Spear // Hallberdier // Hoplit // Ballista // Siege - Ram // Catapult // Trebuchet // Tower

public class UnitSet {

	private int archers = 0;
	private int shortSwordsmen = 0;
	private int longSwordsmen = 0;
	private int twohandedswordman = 0;
	private int spearman = 0;
	private int halberdier = 0;
	private int hoplit = 0;
	private int ballista = 0;
	private int siegeram = 0;
	private int catapult = 0;
	private int trebuchet = 0;
	private int tower = 0;
	private int otherCount = 0;
	private AbstractUnit other = null;
	private Technology technology;

	public static final boolean[] unitTypesAll = { true, true, true, true,
			true, true, true, true, true, true, true, true };
	public static final boolean[][] unitTypesOnlyOne = {
			{ true, false, false, false, false, false, false, false, false,
					false, false, false },
			{ false, true, false, false, false, false, false, false, false,
					false, false, false },
			{ false, false, true, false, false, false, false, false, false,
					false, false, false },
			{ false, false, false, true, false, false, false, false, false,
					false, false, false },
			{ false, false, false, false, true, false, false, false, false,
					false, false, false },
			{ false, false, false, false, false, true, false, false, false,
					false, false, false },
			{ false, false, false, false, false, false, true, false, false,
					false, false, false },
			{ false, false, false, false, false, false, false, true, false,
					false, false, false },
			{ false, false, false, false, false, false, false, false, true,
					false, false, false },
			{ false, false, false, false, false, false, false, false, false,
					true, false, false },
			{ false, false, false, false, false, false, false, false, false,
					false, true, false },
			{ false, false, false, false, false, false, false, false, false,
					false, false, true } };

	// *********************************************
	// GETTER/SETTER:
	
	public void setOther(AbstractUnit other, int count) {
		this.other=other;
		this.otherCount=count;
	}
	
	
	public int getLongSwordsmen() {
		return longSwordsmen;
	}

	public void setLongSwordsmen(int longSwordsmen) {
		this.longSwordsmen = longSwordsmen;
	}

	public int getShortSwordsmen() {
		return shortSwordsmen;
	}

	public void setShortSwordsmen(int shortSwordsmen) {
		this.shortSwordsmen = shortSwordsmen;
	}

	public int getArchers() {
		return archers;
	}

	public void setArchers(int archers) {
		this.archers = archers;
	}

	public int getBallista() {
		return ballista;
	}

	public void setBallista(int ballista) {
		this.ballista = ballista;
	}

	public int getCatapult() {
		return catapult;
	}

	public void setCatapult(int catapult) {
		this.catapult = catapult;
	}

	public int getTrebuchet() {
		return trebuchet;
	}

	public void setTrebuchet(int trebuchet) {
		this.trebuchet = trebuchet;
	}

	public int getTwohandedswordman() {
		return twohandedswordman;
	}

	public void setTwohandedswordman(int twohandedswordman) {
		this.twohandedswordman = twohandedswordman;
	}

	public int getSpearman() {
		return spearman;
	}

	public void setSpearman(int spearman) {
		this.spearman = spearman;
	}

	public int getHalberdier() {
		return halberdier;
	}

	public void setHalberdier(int halberdier) {
		this.halberdier = halberdier;
	}

	public int getHoplit() {
		return hoplit;
	}

	public void setHoplit(int hoplit) {
		this.hoplit = hoplit;
	}

	public int getSiegeram() {
		return siegeram;
	}

	public void setSiegeram(int siegeram) {
		this.siegeram = siegeram;
	}

	public int getTower() {
		return tower;
	}

	public void setTower(int tower) {
		this.tower = tower;
	}

	// *********************************************

	public UnitSet(Technology technology) {
		this.technology = technology;
	}

	public UnitSet(int archers, int shortSwordsmen, int longSwordsmen,
			int twohandedswordman, int spearman, int halberdier, int hoplit,
			int ballista, int siegeram, int catapult, int trebuchet, int tower, Technology technology) {
		this.archers = archers;
		this.shortSwordsmen = shortSwordsmen;
		this.longSwordsmen = longSwordsmen;
		this.twohandedswordman = twohandedswordman;
		this.spearman = spearman;
		this.halberdier = halberdier;
		this.hoplit = hoplit;
		this.ballista = ballista;
		this.siegeram = siegeram;
		this.catapult = catapult;
		this.trebuchet = trebuchet;
		this.tower = tower;
		this.technology = technology;
	}
	
	
	public void clear() {
		this.archers = 0;
		this.shortSwordsmen =0;
		this.longSwordsmen =0;
		this.twohandedswordman = 0;
		this.spearman =0;
		this.halberdier =0;
		this.hoplit = 0;
		this.ballista = 0;
		this.siegeram = 0;
		this.catapult = 0;
		this.trebuchet = 0;
		this.tower = 0;
	}

	// --

	public String toLongString() {
		return archers + " Archers, " + shortSwordsmen + " Short - Swordman, "
				+ longSwordsmen + " Long - Swordman, " + twohandedswordman
				+ " Two Handen - Swordman, " + spearman + " Spearman, "
				+ halberdier + " Halberdier, " + hoplit + " Hoplit, "
				+ ballista + " Ballista, " + siegeram + " Siegeram, "
				+ catapult + " Catapult, " + trebuchet + " Trebuchet, " + tower
				+ " Tower";
	}

	public String toString() {
		String str = "";
		if (archers != 0)
			str = str + archers + " " + Game.getString(R.string.unit_archer)
					+ ", ";
		if (shortSwordsmen != 0)
			str = str + shortSwordsmen + " "
					+ Game.getString(R.string.unit_shortswordman) + ", ";
		if (longSwordsmen != 0)
			str = str + longSwordsmen + " "
					+ Game.getString(R.string.unit_longswordman) + ", ";
		if (twohandedswordman != 0)
			str = str + twohandedswordman + " "
					+ Game.getString(R.string.unit_twohandedswordman) + ", ";
		if (spearman != 0)
			str = str + spearman + " " + Game.getString(R.string.unit_spearman)
					+ ", ";
		if (halberdier != 0)
			str = str + halberdier + " "
					+ Game.getString(R.string.unit_halberdier) + ", ";
		if (hoplit != 0)
			str = str + hoplit + " " + Game.getString(R.string.unit_hoplit)
					+ ", ";
		if (ballista != 0)
			str = str + ballista + " " + Game.getString(R.string.unit_ballista)
					+ ", ";
		if (siegeram != 0)
			str = str + siegeram + " " + Game.getString(R.string.unit_siegeram)
					+ ", ";
		if (catapult != 0)
			str = str + catapult + " " + Game.getString(R.string.unit_catapult)
					+ ", ";
		if (trebuchet != 0)
			str = str + trebuchet + " "
					+ Game.getString(R.string.unit_trebuchet) + ", ";
		if (tower != 0)
			str = str + tower + " " + Game.getString(R.string.unit_tower)
					+ ", ";
		if (otherCount != 0)
			str = str + otherCount + " " + other.getName()
					+ ", ";
		if (!str.equals("")) {
			str = str.substring(0, str.length() - 2);
		}
		return str;
	}

	// Neue Einheiten hier nicht Implementier
	public void setRandom(int max, boolean[] useUnits) {
		Random ra = new Random();
		if (useUnits.length != 6)
			return;
		if (useUnits[0])
			longSwordsmen = ra.nextInt(max);
		if (useUnits[1])
			shortSwordsmen = ra.nextInt(max);
		if (useUnits[2])
			archers = ra.nextInt(max);
		if (useUnits[3])
			ballista = ra.nextInt(max);
		if (useUnits[4])
			catapult = ra.nextInt(max);
		if (useUnits[5])
			trebuchet = ra.nextInt(max);
	}

	// Neue Einheiten hier nicht Implementier
	public void increaseRandom(int maxGrowth, boolean[] useUnits) {
		Random ra = new Random();
		if (useUnits.length != 6)
			return;
		if (useUnits[0])
			longSwordsmen = longSwordsmen + ra.nextInt(maxGrowth);
		if (useUnits[1])
			shortSwordsmen = shortSwordsmen + ra.nextInt(maxGrowth);
		if (useUnits[2])
			archers = archers + ra.nextInt(maxGrowth);
		if (useUnits[3])
			ballista = ballista + ra.nextInt(maxGrowth);
		if (useUnits[4])
			catapult = catapult + ra.nextInt(maxGrowth);
		if (useUnits[5])
			trebuchet = trebuchet + ra.nextInt(maxGrowth);
		// System.out.println("GROWTH -> " + this.toString());
	}

	// Neue Einheiten hier nicht Implementier
	public void decreaseRandom(int maxGrowth, boolean[] useUnits) {
		Random ra = new Random();
		if (useUnits.length != 6)
			return;
		if (useUnits[0])
			longSwordsmen = longSwordsmen - ra.nextInt(maxGrowth);
		if (longSwordsmen < 0)
			longSwordsmen = 0;
		if (useUnits[1])
			shortSwordsmen = shortSwordsmen - ra.nextInt(maxGrowth);
		if (shortSwordsmen < 0)
			shortSwordsmen = 0;
		if (useUnits[2])
			archers = archers - ra.nextInt(maxGrowth);
		if (archers < 0)
			archers = 0;
		if (useUnits[3])
			ballista = ballista - ra.nextInt(maxGrowth);
		if (ballista < 0)
			ballista = 0;
		if (useUnits[4])
			catapult = catapult - ra.nextInt(maxGrowth);
		if (catapult < 0)
			catapult = 0;
		if (useUnits[5])
			trebuchet = trebuchet - ra.nextInt(maxGrowth);
		if (trebuchet < 0)
			trebuchet = 0;
	}

	public UnitGroup makeUnitGroup() {
		UnitGroup g = new UnitGroup(technology);
		if (archers != 0) {
			g.addArchers(archers);
		}
		if (shortSwordsmen != 0) {
			g.addShortSwordsmen(shortSwordsmen);
		}
		if (longSwordsmen != 0) {
			g.addLongSwordsmen(longSwordsmen);
		}
		if (twohandedswordman != 0) {
			g.addTwoHandedSwordman(twohandedswordman);
		}
		if (spearman != 0) {
			g.addSpearman(spearman);
		}
		if (halberdier != 0) {
			g.addHalberdier(halberdier);
		}
		if (hoplit != 0) {
			g.addHoplit(hoplit);
		}
		if (ballista != 0) {
			g.addBallista(ballista);
		}
		if (siegeram != 0) {
			g.addSiegeRam(siegeram);
		}
		if (catapult != 0) {
			g.addCatapult(catapult);
		}
		if (trebuchet != 0) {
			g.addTrebuchet(trebuchet);
		}
		if (tower != 0) {
			g.addTower(tower);
		}
		if (otherCount !=0) {
			g.addOther(other,otherCount);
		}
		return g;
	}

	public int getSum() {
		return longSwordsmen + shortSwordsmen + archers + ballista + catapult
				+ trebuchet+ halberdier + hoplit + siegeram + spearman + tower + twohandedswordman;
	}

	// Neue Einheiten hier nicht Implementier
	public static String unitTypesToString(boolean[] unitTypes) {
		String tmp = "";
		if (unitTypes[0])
			tmp = tmp + "L ";
		if (unitTypes[1])
			tmp = tmp + "S ";
		if (unitTypes[2])
			tmp = tmp + "A ";
		if (unitTypes[3])
			tmp = tmp + "B ";
		if (unitTypes[4])
			tmp = tmp + "C ";
		if (unitTypes[5])
			tmp = tmp + "T ";
		return tmp;
	}

	public void addUnit(AbstractUnit u) {
		if (u instanceof Archer) {
			this.archers++;
		} else if (u instanceof ShortSwordsman) {
			this.shortSwordsmen++;
		} else if (u instanceof LongSwordsman) {
			this.longSwordsmen++;
		} else if (u instanceof TwoHandedSwordman) {
			this.twohandedswordman++;
		} else if (u instanceof Spearman) {
			this.spearman++;
		} else if (u instanceof Halberdier) {
			this.halberdier++;
		} else if (u instanceof Hoplit) {
			this.hoplit++;
		} else if (u instanceof Ballista) {
			this.ballista++;
		} else if (u instanceof SiegeRam) {
			this.siegeram++;
		} else if (u instanceof Catapult) {
			this.catapult++;
		} else if (u instanceof Trebuchet) {
			this.trebuchet++;
		} else if (u instanceof Tower) {
			this.tower++;
		}
	}
	
	public void plusUnitSet(UnitSet u) {
		this.archers = this.archers + u.getArchers(); 
		this.shortSwordsmen = this.shortSwordsmen + u.getShortSwordsmen();
		this.longSwordsmen = this.longSwordsmen + u.getLongSwordsmen();
		this.twohandedswordman = this.twohandedswordman + u.getTwohandedswordman();
		this.spearman = this.spearman + u.getSpearman();
		this.halberdier = this.halberdier + u.getHalberdier();
		this.hoplit = this.hoplit + u.getHoplit();
		this.ballista = this.ballista + u.getBallista();
		this.siegeram = this.siegeram + u.getSiegeram();
		this.catapult = this.catapult + u.getCatapult();
		this.trebuchet = this.trebuchet + u.getTrebuchet();
		this.tower = this.tower + u.getTower();
	}
	
	
	public void plusUnitSetPercent(UnitSet u, int p) {
		this.archers = this.archers + u.getArchers() * p/100; 
		this.shortSwordsmen = this.shortSwordsmen + u.getShortSwordsmen() * p/100;
		this.longSwordsmen = this.longSwordsmen + u.getLongSwordsmen() * p/100;
		this.twohandedswordman = this.twohandedswordman + u.getTwohandedswordman() * p/100;
		this.spearman = this.spearman + u.getSpearman() * p/100;
		this.halberdier = this.halberdier + u.getHalberdier() * p/100;
		this.hoplit = this.hoplit + u.getHoplit() * p/100;
		this.ballista = this.ballista + u.getBallista() * p/100;
		this.siegeram = this.siegeram + u.getSiegeram() * p/100;
		this.catapult = this.catapult + u.getCatapult() * p/100;
		this.trebuchet = this.trebuchet + u.getTrebuchet() * p/100;
		this.tower = this.tower + u.getTower();
	}
	
	
	public void minusUnitSet(UnitSet u) {
		//TODO. Negative Zahlen vermeiden...
		this.archers = this.archers - u.getArchers(); 
		this.shortSwordsmen = this.shortSwordsmen - u.getShortSwordsmen();
		this.longSwordsmen = this.longSwordsmen - u.getLongSwordsmen();
		this.twohandedswordman = this.twohandedswordman - u.getTwohandedswordman();
		this.spearman = this.spearman - u.getSpearman();
		this.halberdier = this.halberdier - u.getHalberdier();
		this.hoplit = this.hoplit - u.getHoplit();
		this.ballista = this.ballista - u.getBallista();
		this.siegeram = this.siegeram - u.getSiegeram();
		this.catapult = this.catapult - u.getCatapult();
		this.trebuchet = this.trebuchet - u.getTrebuchet();
		this.tower = this.tower - u.getTower();
	}
	
}
